package com.bcfrj.shop.controller;

import com.bcfrj.shop.pojo.Good;
import com.bcfrj.shop.service.impl.GoodServiceImpl;
import com.bcfrj.shop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodServiceImpl goodService;
    @PostMapping("/addGood")
    public Result addGood(Good good){
        System.out.println(good);
        if (good.getGood_desc().isEmpty()){
            good.setGood_desc("没有设置商品描述");
        }
        Integer rows = goodService.insertGood(good);
        if(rows > 0){
            return Result.ok("添加成功！");
        }
        return Result.fail("添加失败！");
    }

    @GetMapping("/getGoodList")
    public Result getGoodList(){
        List<Good> goodList = goodService.queryGoodAll();
        if (goodList != null){
            return Result.ok(goodList);
        }
        return Result.fail("商品列表获取失败！");
    }

    @GetMapping("/getGood")
    public Result getGood(Integer Good_id) {
        System.out.println(Good_id);
        Good good = goodService.queryGoodById(Good_id);
        if (good != null){
            return Result.ok(good);

        }
        else{
            return Result.fail("查询商品失败，没有这个商品");
        }
    }

    @PostMapping("/updateGood")
    public Result updateGood(Good good){
        Integer rows = goodService.updateGood(good);

        if(rows > 0){
            return Result.ok("修改成功");
        }
        else {
            return Result.fail("修改失败");
        }
    }

    @PostMapping("/deleteGood")
    public Result deleteGood( Integer id){
        Integer rows = goodService.deleteGood(id);
        if(rows > 0){
            return Result.ok("删除成功");
        }
        else {
            return Result.fail("删除失败");
        }
    }
}
